package com.zhb.tools

import com.google.gson.Gson
import com.google.gson.JsonParseException
import com.google.gson.JsonParser
import org.json.JSONObject
import java.io.UnsupportedEncodingException
import java.lang.reflect.Type
import java.net.URLEncoder

enum class JsonUtils {
    INSTANCE;

    /**
     * 获取Gson对象
     *
     * @return
     */
    var gson: Gson? = null

    companion object {
        private var IS_DEBUG = true

        /**
         * 把json字符串转换成实体对象
         *
         * @param obj
         * @param type
         * @return
         */
        fun fromJson(obj: String?, type: Type?): Any? {
            val result: Any?
            result = if (IS_DEBUG) {
                INSTANCE.gson!!.fromJson(obj, type)
            } else {
                try {
                    INSTANCE.gson!!.fromJson<Any>(obj, type)
                } catch (e: Exception) {
                    null
                }
            }
            return result
        }

        /**
         * 把json对象转换成实体对象
         *
         * @param obj
         * @param type
         * @return
         */
        fun fromJson(obj: Any?, type: Type?): Any? {
            val result: Any?
            result = if (IS_DEBUG) {
                INSTANCE.gson!!.fromJson(INSTANCE.gson!!.toJson(obj), type)
            } else {
                try {
                    INSTANCE.gson!!.fromJson<Any>(INSTANCE.gson!!.toJson(obj), type)
                } catch (e: Exception) {
                    null
                }
            }
            return result
        }

        /**
         * 把实体对象转换成json字符串
         *
         * @param obj
         * @return
         */
        fun toJson(obj: Any?): String? {
            val result: String?
            result = try {
                INSTANCE.gson!!.toJson(obj)
            } catch (e: Exception) {
                null
            }
            return result
        }

        /**
         * 把实体对象按照类型转成成json字符串
         *
         * @param obj
         * @param tpye
         * @return
         */
        fun toJson(obj: Any?, tpye: Type?): String? {
            val result: String?
            result = try {
                INSTANCE.gson!!.toJson(obj, tpye)
            } catch (e: Exception) {
                null
            }
            return result
        }

        /**
         * 判断是否是json
         *
         * @param str
         * @return
         */
        fun isJson(str: String?): Boolean {
            return try {
                JsonParser().parse(str)
                true
            } catch (e: JsonParseException) {
                false
            }
        }

        fun jsonToStr(`object`: JSONObject?): String {
            if (`object` == null) return ""
            val sb = StringBuffer()
            val keys = `object`.keys()
            try {
                if (keys.hasNext()) {
                    val key = keys.next()
                    val value = URLEncoder.encode(`object`.opt(key).toString(), "UTF-8")
                    sb.append(key).append("=").append(value)
                }
                while (keys.hasNext()) {
                    val key = keys.next()
                    val value = URLEncoder.encode(`object`.opt(key).toString(), "UTF-8")
                    sb.append("&").append(key).append("=").append(value)
                }
            } catch (e: UnsupportedEncodingException) {
                e.printStackTrace()
            }
            return sb.toString()
        }

        /**
         * 设置转换器是否调试模式
         *
         * @param isDebug
         */
        fun setIsDebug(isDebug: Boolean) {
            IS_DEBUG = isDebug
        }
    }

    init {
        gson = Gson()
    }
}